<?php
namespace Mobile\Controller;
use Think\Controller;
class FinancialController extends CommunalController{
    //项目款项 designed by:zhangsen 2018-05-23
    function project_funds(){
        parent::check_permission('KX');
        //赋值
        if(!empty($_REQUEST['mode'])){$mode=$_REQUEST['mode'];}else{$mode=1;}
        $this->assign('mode',$mode);
        if(get_permission('KX',2)){
            if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=$this->login_center_id;}
            $this->assign('center_id',$center_id);
        }else{
            if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=-10;}
            $this->assign('center_id',$center_id);
        }
        if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=-10;}
        $this->assign('type',$type);
        if(!empty($_REQUEST['ptype'])){$ptype=$_REQUEST['ptype'];}else{$ptype=-10;}
        $this->assign('ptype',$ptype);

        if(get_permission('KX',2)){
            //所属中心下拉
            $pro_list=$this->get_db_universal_type(18,NULL,NULL,NULL,NULL,$this->login_center_id);
            $this->assign('pro_list',$pro_list);
        }else{
            //所属中心下拉
            $pro_list=$this->get_db_universal_type(18);
            $this->assign('pro_list',$pro_list);
        }
        if($center_id!=-10){
            $db_universal_type=M('db_universal_type');
            $where_c['type']=18;
            $where_c['name2']=$center_id;
            $center_name=$db_universal_type->field('id,name1')->where($where_c)->find();
            $this->assign('center_name',$center_name['name1']);
        }
        //项目分类下拉菜单
        $ptype_list=$this->library_type_list(6,22);
        $this->assign('ptype_list',$ptype_list[0]['second']);
        if($ptype!=-10){
            $db_universal_label2=M('db_universal_label2');
            $where_p['id']=$ptype;
            $ptype_name=$db_universal_label2->where($where_p)->find();
            preg_match_all("/./us",$ptype_name['second_name'],$matchs);
            $ptype_name_length=count($matchs[0]);
            $this->assign('ptype_name',$ptype_name['second_name']);
            $this->assign('ptype_name_length',$ptype_name_length);
        }
        $db_project_seach=M('db_project_seach');
        if(!empty($_REQUEST['keyword']))
        {
            $map['project_num']=array('like','%'.$_REQUEST['keyword'].'%');
            $map['project_name']=array('like','%'.$_REQUEST['keyword'].'%');
            $map['_logic']='or';
            $where['_complex']=$map;
            $this->assign('keyword',$_REQUEST['keyword']);

            $add_where['center_id']=130010;
            $add_where['keyword']=$_REQUEST['keyword'];
            $add_where['add_uid']=$this->login_id;
            $select_res=$db_project_seach->where($add_where)->find();
            if(empty($select_res)){
                $add_where['add_date']=date('Y-m-d H:i:s');
                $db_project_seach->add($add_where);
            }
        }
        if($center_id!=-10){$where['center_id']=$center_id;}
        if($ptype!=-10){$where['p_type']=$ptype;}
        $db_project=M('db_project');
        $db_project_funds=M('db_project_funds');
        $db_project_funds_stage=M('db_project_funds_stage');
        $db_project_funds_ticket=M('db_project_funds_ticket');
        $db_project_funds_money=M('db_project_funds_money');
        $pro=$db_project->where($where)->field('db_project.*')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->select();
        foreach($pro as $ka=>$va){
            if($ka==0){
                $projects=$va['id'];
            }else{
                $projects.=','.$va['id'];
            }
        }
        if($mode==1){
            if($type==-10){
                $where_s['db_project_funds.pid']=array('in',$projects);
                $fp=$db_project_funds_ticket->field('db_project_funds_ticket.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds.id as fid')->where($where_s)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('id desc')->select();
                $fk=$db_project_funds_money->field('db_project_funds_money.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds_ticket.id as kid,db_project_funds_ticket.money as fmoney,db_project_funds_ticket.date as fdate,db_project_funds.id as fid,invoice')->where($where_s)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('id desc')->select();
                $detail = array_merge((array)$fp,(array)$fk);
                $result=$this->multi_array_sort($detail,'add_date',SORT_DESC);
                foreach($result as $key=>$value){
                    unset($stag);
                    $result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
                    $result[$key]['add_user']=$this->get_username($value['add_uid']);
                    if(!empty($value['tid'])){
                        //开票
                        $where_kp['tid']=$value['tid'];
                        $where_kp['add_date']=array('elt',$value['add_date']);
                        $ykfp=$db_project_funds_ticket->where($where_kp)->sum('money');
                        if(($value['ticket_money']-$ykfp)>=0){$result[$key]['syykp']=$value['ticket_money']-$ykfp;}else{$result[$key]['syykp']=0;}
                        $result[$key]['fmoney']=$value['money'];
                        $result[$key]['fdate']=$value['date'];
                        //能否删除
                        $where_del['id']=$value['tid'];
                        $max_stage=$db_project_funds_stage->where($where_del)->find();
                        $where_da['mid']=$max_stage['mid'];
                        $stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
                        foreach($stages as $kk=>$vv){
                            if($kk==0){
                                $stag=$vv['id'];
                            }else{
                                $stag.=','.$vv['id'];
                            }
                        }
                        $where_dele['tid']=array('in',$stag);
                        $dele=$db_project_funds_ticket->where($where_dele)->order('id desc')->find();
                        if($dele['id']==$value['id']){
                            $where_h['sid']=$value['id'];
                            $has=$db_project_funds_money->where($where_h)->find();
                            if(empty($has)){$result[$key]['del']=1;}
                        }
                        //能否编辑
                        $where_e['sid']=$value['id'];
                        $edit=$db_project_funds_money->where($where_e)->find();
                        if(empty($edit)){$result[$key]['edit']=1;}
                    }else{
                        //收款
                        $where_sk['sid']=$value['sid'];
                        $where_sk['add_date']=array('elt',$value['add_date']);
                        $ysk=$db_project_funds_money->where($where_sk)->sum('money');
                        if(($value['fmoney']-$ysk)>=0){$result[$key]['syysk']=$value['fmoney']-$ysk;}else{$result[$key]['syysk']=0;}
                        //能否删除
                        $where_d['id']=$value['sid'];
                        $stage=$db_project_funds_ticket->where($where_d)->find();
                        $where_del['id']=$stage['tid'];
                        $max_stage=$db_project_funds_stage->where($where_del)->find();
                        $where_da['mid']=$max_stage['mid'];
                        $stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
                        foreach($stages as $kk=>$vv){
                            if($kk==0){
                                $stag=$vv['id'];
                            }else{
                                $stag.=','.$vv['id'];
                            }
                        }
                        $where_dele['tid']=array('in',$stag);
                        $dele=$db_project_funds_ticket->field('id')->where($where_dele)->select();
                        foreach($dele as $k3=>$v3){
                            if($k3==0){
                                $sta=$v3['id'];
                            }else{
                                $sta.=','.$v3['id'];
                            }
                        }
                        $where_maxd['sid']=array('in',$sta);
                        $maxdele=$db_project_funds_money->where($where_maxd)->order('id desc')->find();
                        if($maxdele['id']==$value['id']){$result[$key]['del']=1;}
                    }
                }
                $count=count($result);
                $Page = new \Think\Page($count,6);
                $result=array_slice($result, $Page->firstRow,$Page->listRows);
                $show=$Page->show();
            }elseif($type==1){
                $where_s['db_project_funds.pid']=array('in',$projects);
                $count=$db_project_funds_ticket->field('db_project_funds_ticket.id')->where($where_s)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('db_project_funds_ticket.id desc')->count();
                $Page = new \Think\Page($count,6);
                $show=$Page->show();
                $result=$db_project_funds_ticket->field('db_project_funds_ticket.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds.id as fid')->where($where_s)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->limit($Page->firstRow.','.$Page->listRows)->order('db_project_funds_ticket.id desc')->select();
                foreach($result as $key=>$value){
                    unset($stag);
                    $result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
                    $result[$key]['add_user']=$this->get_username($value['add_uid']);
                    //开票
                    $where_kp['tid']=$value['tid'];
                    $where_kp['add_date']=array('elt',$value['add_date']);
                    $ykfp=$db_project_funds_ticket->where($where_kp)->sum('money');
                    if(($value['ticket_money']-$ykfp)>=0){$result[$key]['syykp']=$value['ticket_money']-$ykfp;}else{$result[$key]['syykp']=0;}
                    $result[$key]['fmoney']=$value['money'];
                    $result[$key]['fdate']=$value['date'];
                    //能否删除
                    $where_del['id']=$value['tid'];
                    $max_stage=$db_project_funds_stage->where($where_del)->find();
                    $where_da['mid']=$max_stage['mid'];
                    $stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
                    foreach($stages as $kk=>$vv){
                        if($kk==0){
                            $stag=$vv['id'];
                        }else{
                            $stag.=','.$vv['id'];
                        }
                    }
                    $where_dele['tid']=array('in',$stag);
                    $dele=$db_project_funds_ticket->where($where_dele)->order('id desc')->find();
                    if($dele['id']==$value['id']){
                        $where_h['sid']=$value['id'];
                        $has=$db_project_funds_money->where($where_h)->find();
                        if(empty($has)){$result[$key]['del']=1;}
                    }
                    //能否编辑
                    $where_e['sid']=$value['id'];
                    $edit=$db_project_funds_money->where($where_e)->find();
                    if(empty($edit)){$result[$key]['edit']=1;}
                }
            }elseif($type==2){
                $where_s['db_project_funds.pid']=array('in',$projects);
                $count=$db_project_funds_money->field('db_project_funds_money.id')->where($where_s)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->count();
                $Page = new \Think\Page($count,6);
                $show=$Page->show();
                $result=$db_project_funds_money->field('db_project_funds_money.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds_ticket.money as fmoney,db_project_funds_ticket.id as kid,db_project_funds_ticket.date as fdate,db_project_funds.id as fid,invoice')->where($where_s)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->limit($Page->firstRow.','.$Page->listRows)->order('db_project_funds_money.id desc')->select();
                foreach($result as $key=>$value){
                    unset($stag);
                    $result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
                    $result[$key]['add_user']=$this->get_username($value['add_uid']);
                    //收款
                    $where_sk['sid']=$value['sid'];
                    $where_sk['add_date']=array('elt',$value['add_date']);
                    $ysk=$db_project_funds_money->where($where_sk)->sum('money');
                    if(($value['fmoney']-$ysk)>=0){$result[$key]['syysk']=$value['fmoney']-$ysk;}else{$result[$key]['syysk']=0;}
                    //能否删除
                    $where_d['id']=$value['sid'];
                    $stage=$db_project_funds_ticket->where($where_d)->find();
                    $where_del['id']=$stage['tid'];
                    $max_stage=$db_project_funds_stage->where($where_del)->find();
                    $where_da['mid']=$max_stage['mid'];
                    $stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
                    foreach($stages as $kk=>$vv){
                        if($kk==0){
                            $stag=$vv['id'];
                        }else{
                            $stag.=','.$vv['id'];
                        }
                    }
                    $where_dele['tid']=array('in',$stag);
                    $dele=$db_project_funds_ticket->field('id')->where($where_dele)->select();
                    foreach($dele as $k3=>$v3){
                        if($k3==0){
                            $sta=$v3['id'];
                        }else{
                            $sta.=','.$v3['id'];
                        }
                    }
                    $where_maxd['sid']=array('in',$sta);
                    $maxdele=$db_project_funds_money->where($where_maxd)->order('id desc')->find();
                    if($maxdele['id']==$value['id']){$result[$key]['del']=1;}
                }
            }
        }else{
            $where_s['db_project_funds.pid']=array('in',$projects);
            $count=$db_project_funds->field('db_project_funds.id')->where($where_s)->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->count();
            $Page = new \Think\Page($count,6);
            $show=$Page->show();
            $result=$db_project_funds->field('db_project_funds.*,project_num,project_name,p_type')->where($where_s)->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->limit($Page->firstRow.','.$Page->listRows)->order('db_project_funds.id desc')->select();
            foreach($result as $key=>$value){
                $result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
                $result[$key]['add_user']=$this->get_username($value['add_uid']);
            }
        }
        //推荐搜索
        $where_aaa['center_id']=130010;
        $result_t=$db_project_seach->field('keyword,count(id) as ct')->where($where_aaa)->group('center_id,keyword')->order('ct desc,add_date desc')->limit(6)->select();
        $this->assign('result_t',$result_t);
        //搜索历史
        $where_l['center_id']=130010;
        $where_l['add_uid']=$this->login_id;
        $result_l=$db_project_seach->field('keyword,max(add_date) as md')->where($where_l)->group('center_id,keyword')->order('md desc')->limit(6)->select();
        $this->assign('result_l',$result_l);
        $this->assign('result',$result);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display();
    }

    //项目款项 designed by:zhangsen 2018-05-23
    function project_funds_ajax(){
        $start_size=$_REQUEST['start_size'];
        $start_page=$_REQUEST['start_page'];
        parent::check_permission('KX');
        //赋值
        if(!empty($_REQUEST['mode'])){$mode=$_REQUEST['mode'];}else{$mode=1;}
        $this->assign('mode',$mode);
        if(get_permission('KX',2)){
            if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=$this->login_center_id;}
            $this->assign('center_id',$center_id);
        }else{
            if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=-10;}
            $this->assign('center_id',$center_id);
        }
        if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=-10;}
        $this->assign('type',$type);
        if(!empty($_REQUEST['ptype'])){$ptype=$_REQUEST['ptype'];}else{$ptype=-10;}
        $this->assign('ptype',$ptype);

        if(get_permission('KX',2)){
            //所属中心下拉
            $pro_list=$this->get_db_universal_type(18,NULL,NULL,NULL,NULL,$this->login_center_id);
            $this->assign('pro_list',$pro_list);
        }else{
            //所属中心下拉
            $pro_list=$this->get_db_universal_type(18);
            $this->assign('pro_list',$pro_list);
        }
        if($center_id!=-10){
            $db_universal_type=M('db_universal_type');
            $where_c['type']=18;
            $where_c['name2']=$center_id;
            $center_name=$db_universal_type->field('id,name1')->where($where_c)->find();
            $this->assign('center_name',$center_name['name1']);
        }
        //项目分类下拉菜单
        $ptype_list=$this->library_type_list(6,22);
        $this->assign('ptype_list',$ptype_list[0]['second']);
        if($ptype!=-10){
            $db_universal_label2=M('db_universal_label2');
            $where_p['id']=$ptype;
            $ptype_name=$db_universal_label2->where($where_p)->find();
            $this->assign('ptype_name',$ptype_name['second_name']);
        }
        if(!empty($_REQUEST['keyword']))
        {
            $map['project_num']=array('like','%'.$_REQUEST['keyword'].'%');
            $map['project_name']=array('like','%'.$_REQUEST['keyword'].'%');
            $map['_logic']='or';
            $where['_complex']=$map;
            $this->assign('keyword',$_REQUEST['keyword']);
        }
        if($center_id!=-10){$where['center_id']=$center_id;}
        if($ptype!=-10){$where['p_type']=$ptype;}
        $db_project=M('db_project');
        $db_project_funds=M('db_project_funds');
        $db_project_funds_stage=M('db_project_funds_stage');
        $db_project_funds_ticket=M('db_project_funds_ticket');
        $db_project_funds_money=M('db_project_funds_money');
        $pro=$db_project->where($where)->field('db_project.*')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->select();
        foreach($pro as $ka=>$va){
            if($ka==0){
                $projects=$va['id'];
            }else{
                $projects.=','.$va['id'];
            }
        }
        if($mode==1){
            if($type==-10){
                $where_s['db_project_funds.pid']=array('in',$projects);
                $fp=$db_project_funds_ticket->field('db_project_funds_ticket.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds.id as fid')->where($where_s)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('id desc')->select();
                $fk=$db_project_funds_money->field('db_project_funds_money.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds_ticket.id as kid,db_project_funds_ticket.money as fmoney,db_project_funds_ticket.date as fdate,db_project_funds.id as fid,invoice')->where($where_s)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('id desc')->select();
                $detail = array_merge((array)$fp,(array)$fk);
                $result=$this->multi_array_sort($detail,'add_date',SORT_DESC);
                foreach($result as $key=>$value){
                    unset($stag);
                    $result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
                    $result[$key]['add_user']=$this->get_username($value['add_uid']);
                    if(!empty($value['tid'])){
                        //开票
                        $where_kp['tid']=$value['tid'];
                        $where_kp['add_date']=array('elt',$value['add_date']);
                        $ykfp=$db_project_funds_ticket->where($where_kp)->sum('money');
                        if(($value['ticket_money']-$ykfp)>=0){$result[$key]['syykp']=$value['ticket_money']-$ykfp;}else{$result[$key]['syykp']=0;}
                        $result[$key]['fmoney']=$value['money']+0;
                        $result[$key]['ticket_money']=$value['ticket_money']+0;
                        $result[$key]['fdate']=$value['date'];
                        //能否删除
                        $where_del['id']=$value['tid'];
                        $max_stage=$db_project_funds_stage->where($where_del)->find();
                        $where_da['mid']=$max_stage['mid'];
                        $stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
                        foreach($stages as $kk=>$vv){
                            if($kk==0){
                                $stag=$vv['id'];
                            }else{
                                $stag.=','.$vv['id'];
                            }
                        }
                        $where_dele['tid']=array('in',$stag);
                        $dele=$db_project_funds_ticket->where($where_dele)->order('id desc')->find();
                        if($dele['id']==$value['id']){
                            $where_h['sid']=$value['id'];
                            $has=$db_project_funds_money->where($where_h)->find();
                            if(empty($has)){$result[$key]['del']=1;}
                        }
                        //能否编辑
                        $where_e['sid']=$value['id'];
                        $edit=$db_project_funds_money->where($where_e)->find();
                        if(empty($edit)){$result[$key]['edit']=1;}
                    }else{
                        //收款
                        $where_sk['sid']=$value['sid'];
                        $where_sk['add_date']=array('elt',$value['add_date']);
                        $ysk=$db_project_funds_money->where($where_sk)->sum('money');
                        if(($value['fmoney']-$ysk)>=0){$result[$key]['syysk']=$value['fmoney']-$ysk;}else{$result[$key]['syysk']=0;}
                        //能否删除
                        $where_d['id']=$value['sid'];
                        $stage=$db_project_funds_ticket->where($where_d)->find();
                        $where_del['id']=$stage['tid'];
                        $max_stage=$db_project_funds_stage->where($where_del)->find();
                        $where_da['mid']=$max_stage['mid'];
                        $stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
                        foreach($stages as $kk=>$vv){
                            if($kk==0){
                                $stag=$vv['id'];
                            }else{
                                $stag.=','.$vv['id'];
                            }
                        }
                        $where_dele['tid']=array('in',$stag);
                        $dele=$db_project_funds_ticket->field('id')->where($where_dele)->select();
                        foreach($dele as $k3=>$v3){
                            if($k3==0){
                                $sta=$v3['id'];
                            }else{
                                $sta.=','.$v3['id'];
                            }
                        }
                        $where_maxd['sid']=array('in',$sta);
                        $maxdele=$db_project_funds_money->where($where_maxd)->order('id desc')->find();
                        if($maxdele['id']==$value['id']){$result[$key]['del']=1;}
                    }
                }
                $count=count($result);
                $Page = new \Think\Page($count,6);
                $result=array_slice($result, $start_size,$start_page);
                $show=$Page->show();
            }elseif($type==1){
                $where_s['db_project_funds.pid']=array('in',$projects);
                $count=$db_project_funds_ticket->field('db_project_funds_ticket.id')->where($where_s)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('db_project_funds_ticket.id desc')->count();
                $Page = new \Think\Page($count,6);
                $show=$Page->show();
                $result=$db_project_funds_ticket->field('db_project_funds_ticket.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds.id as fid')->where($where_s)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->limit($start_size.','.$start_page)->order('db_project_funds_ticket.id desc')->select();
                foreach($result as $key=>$value){
                    unset($stag);
                    $result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
                    $result[$key]['add_user']=$this->get_username($value['add_uid']);
                    //开票
                    $where_kp['tid']=$value['tid'];
                    $where_kp['add_date']=array('elt',$value['add_date']);
                    $ykfp=$db_project_funds_ticket->where($where_kp)->sum('money');
                    if(($value['ticket_money']-$ykfp)>=0){$result[$key]['syykp']=$value['ticket_money']-$ykfp;}else{$result[$key]['syykp']=0;}
                    $result[$key]['fmoney']=$value['money']+0;
                    $result[$key]['ticket_money']=$value['ticket_money']+0;
                    $result[$key]['fdate']=$value['date'];
                    $result[$key]['ticket_money']=$value['ticket_money']+0;
                    //能否删除
                    $where_del['id']=$value['tid'];
                    $max_stage=$db_project_funds_stage->where($where_del)->find();
                    $where_da['mid']=$max_stage['mid'];
                    $stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
                    foreach($stages as $kk=>$vv){
                        if($kk==0){
                            $stag=$vv['id'];
                        }else{
                            $stag.=','.$vv['id'];
                        }
                    }
                    $where_dele['tid']=array('in',$stag);
                    $dele=$db_project_funds_ticket->where($where_dele)->order('id desc')->find();
                    if($dele['id']==$value['id']){
                        $where_h['sid']=$value['id'];
                        $has=$db_project_funds_money->where($where_h)->find();
                        if(empty($has)){$result[$key]['del']=1;}
                    }
                    //能否编辑
                    $where_e['sid']=$value['id'];
                    $edit=$db_project_funds_money->where($where_e)->find();
                    if(empty($edit)){$result[$key]['edit']=1;}
                }
            }elseif($type==2){
                $where_s['db_project_funds.pid']=array('in',$projects);
                $count=$db_project_funds_money->field('db_project_funds_money.id')->where($where_s)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->count();
                $Page = new \Think\Page($count,6);
                $show=$Page->show();
                $result=$db_project_funds_money->field('db_project_funds_money.*,stage,project_num,project_name,p_type,ticket_money,ticket_date,db_project_funds_ticket.money as fmoney,db_project_funds_ticket.id as kid,db_project_funds_ticket.date as fdate,db_project_funds.id as fid,invoice')->where($where_s)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->limit($start_size.','.$start_page)->order('db_project_funds_money.id desc')->select();
                foreach($result as $key=>$value){
                    unset($stag);
                    $result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
                    $result[$key]['add_user']=$this->get_username($value['add_uid']);
                    //收款
                    $where_sk['sid']=$value['sid'];
                    $where_sk['add_date']=array('elt',$value['add_date']);
                    $ysk=$db_project_funds_money->where($where_sk)->sum('money');
                    if(($value['fmoney']-$ysk)>=0){$result[$key]['syysk']=$value['fmoney']-$ysk;}else{$result[$key]['syysk']=0;}
                    //能否删除
                    $where_d['id']=$value['sid'];
                    $stage=$db_project_funds_ticket->where($where_d)->find();
                    $where_del['id']=$stage['tid'];
                    $max_stage=$db_project_funds_stage->where($where_del)->find();
                    $where_da['mid']=$max_stage['mid'];
                    $stages=$db_project_funds_stage->where($where_da)->order('stage')->select();
                    foreach($stages as $kk=>$vv){
                        if($kk==0){
                            $stag=$vv['id'];
                        }else{
                            $stag.=','.$vv['id'];
                        }
                    }
                    $where_dele['tid']=array('in',$stag);
                    $dele=$db_project_funds_ticket->field('id')->where($where_dele)->select();
                    foreach($dele as $k3=>$v3){
                        if($k3==0){
                            $sta=$v3['id'];
                        }else{
                            $sta.=','.$v3['id'];
                        }
                    }
                    $where_maxd['sid']=array('in',$sta);
                    $maxdele=$db_project_funds_money->where($where_maxd)->order('id desc')->find();
                    if($maxdele['id']==$value['id']){$result[$key]['del']=1;}
                }
            }
        }else{
            $where_s['db_project_funds.pid']=array('in',$projects);
            $count=$db_project_funds->field('db_project_funds.id')->where($where_s)->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->count();
            $Page = new \Think\Page($count,6);
            $show=$Page->show();
            $result=$db_project_funds->field('db_project_funds.*,project_num,project_name,p_type')->where($where_s)->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->limit($start_size.','.$start_page)->order('db_project_funds.id desc')->select();
            foreach($result as $key=>$value){
                $result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
                $result[$key]['add_user']=$this->get_username($value['add_uid']);
            }
        }
        echo json_encode($result);die;
    }

    //项目款项-->单据详情 designed by:wuhuili 2018-06-25
    function project_funds_detail_dj(){
        $db_project_funds=M('db_project_funds');
        $db_project_funds_stage=M('db_project_funds_stage');
        $db_project_funds_ticket=M('db_project_funds_ticket');
        $db_project_funds_money=M('db_project_funds_money');
        //全部款项阶段
        $where_a['db_project_funds.id']=$_REQUEST['fid'];
        $main=$db_project_funds->field('db_project_funds.*,project_num,project_name,center_id')->where($where_a)->join('left join db_project on db_project_funds.pid=db_project.id')->find();
        $type_list=$this->get_db_universal_type(3,1,NULL,NULL,NULL,$main['center_id']);
        $main['center_name']=$type_list['name1'];
        $main['contract_money']=$main['contract_money']?$main['contract_money']+0:'';
        $main['add_user']=$this->get_username($main['add_uid']);
        $where_f['mid']=$_REQUEST['fid'];
        $main['stage']=$db_project_funds_stage->where($where_f)->select();
        foreach($main['stage'] as $kk=>$vv){
            $main['stage'][$kk]['bili']=$vv['bili']?($vv['bili']+0).'%':'';
            $main['stage'][$kk]['stage_money']=$vv['stage_money']+0;
            $main['stage'][$kk]['ticket_money']=$vv['ticket_money']+0;
        }
        $this->assign('main',$main);
        //项目开票详情
        if(!empty($_REQUEST['tid'])){
            $where_b['db_project_funds_ticket.id']=$_REQUEST['id'];
            $data2=$db_project_funds_ticket->field('db_project_funds_ticket.*,stage,project_num,project_name,ticket_money,ticket_date,db_project_funds.id as fid,contract_money,contract_date,db_project_funds.file_name,db_project_funds.file_url')->where($where_b)->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->find();
            //已开票金额
            $where_s['tid']=$data2['tid'];
            $where_s['id']=array('neq',$data2['id']);
            $ykp=$db_project_funds_ticket->where($where_s)->sum('money');
            if($ykp){$data2['ykp']=$ykp;}else{$data2['ykp']=0;}
            $data2['contract_money']=$data2['contract_money']?$data2['contract_money']+0:'';
            $data2['ticket_money']=$data2['ticket_money']+0;
            $data2['money']=$data2['money']+0;
            $data2['add_user']=$this->get_username($data2['add_uid']);
            $this->assign('ticket',$data2);
        }

        //项目收款详情
        if(!empty($_REQUEST['sid'])){
            $where['db_project_funds_money.id']=$_REQUEST['id'];
            $data3=$db_project_funds_money->field('db_project_funds_money.*,stage,project_num,project_name,ticket_money,ticket_date,db_project_funds_ticket.money as fmoney,db_project_funds_ticket.date as fdate,db_project_funds.id as fid,invoice,contract_money,contract_date,db_project_funds.file_name,db_project_funds.file_url')->where($where)->join('left join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('left join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->join('left join db_project_funds on db_project_funds_stage.mid=db_project_funds.id')->join('left join db_project on db_project_funds.pid=db_project.id')->find();
            //发票已收款金额
            $where_s['sid']=$data3['sid'];
            $where_s['id']=array('neq',$data3['id']);
            $ysk=$db_project_funds_money->where($where_s)->sum('money');
            if($ysk){$data3['ysk']=$ysk;}else{$data3['ysk']=0;}
            $data3['contract_money']=$data3['contract_money']?$data3['contract_money']+0:'';
            $data3['fmoney']=$data3['fmoney']+0;
            $data3['money']=$data3['money']+0;
            $data3['add_user']=$this->get_username($data3['add_uid']);
            $this->assign('money',$data3);
        }
        $url=__APP__."/Mobile/Financial/project_funds/keyword/".$_REQUEST['keyword']."/mode/".$_REQUEST['mode']."/center_id/".$_REQUEST['center_id']."/type/".$_REQUEST['type']."/ptype/".$_REQUEST['ptype'];
        $this->assign('url',$url);
        $this->display();
    }
    //项目款项-->项目详情 designed by:wuhuili 2018-06-25
    function project_funds_detail_xm(){
        $db_project_funds=M('db_project_funds');
        $db_project_funds_stage=M('db_project_funds_stage');
        $db_project_funds_ticket=M('db_project_funds_ticket');
        $db_project_funds_money=M('db_project_funds_money');
        $where_a['db_project_funds.id']=$_REQUEST['id'];
        $result=$db_project_funds->field('db_project_funds.*,project_num,project_name,center_id')->where($where_a)->join('left join db_project on db_project_funds.pid=db_project.id')->find();
        $type_list=$this->get_db_universal_type(3,1,NULL,NULL,NULL,$result['center_id']);
        $result['center_name']=$type_list['name1'];
        $result['contract_money']=$result['contract_money']?$result['contract_money']+0:'';
        $result['add_user']=$this->get_username($result['add_uid']);
        //阶段
        $result['p_typen']=$this->get_db_universal_label(2,$result['p_type']);
        $where_t['mid']=$result['id'];
        $result['two']=$db_project_funds_stage->where($where_t)->order('stage')->select();
        $total=0;
        foreach($result['two'] as $k2=>$v2){
            $result['two'][$k2]['bili']=$v2['bili']?($v2['bili']+0).'%':'';
            $result['two'][$k2]['stage_money']=$v2['stage_money']+0;
            $result['two'][$k2]['ticket_money']=$v2['ticket_money']+0;
            $where_th['tid']=$v2['id'];
            $result['two'][$k2]['three']=$db_project_funds_ticket->where($where_th)->order('id desc')->select();
            $result['two'][$k2]['kp_count']=count($result['two'][$k2]['three']);
            $total=$total+$result['two'][$k2]['kp_count'];
            foreach($result['two'][$k2]['three'] as $k3=>$v3){
                $result['two'][$k2]['three'][$k3]['add_user']=$this->get_username($v3['add_uid']);
                $where_kp['tid']=$v3['tid'];
                $where_kp['add_date']=array('elt',$v3['add_date']);
                $ykfp=$db_project_funds_ticket->where($where_kp)->sum('money');
                if($v2['ticket_money']-$ykfp>=0){$result['two'][$k2]['three'][$k3]['syykp']=$v2['ticket_money']-$ykfp;}else{$result['two'][$k2]['three'][$k3]['syykp']=0;}
                if($v3['tid']==$v2['id']){if($v2['stage']==count($result['two'])){$result['nedit']=1;}}
                $where_f['sid']=$v3['id'];
                $result['two'][$k2]['three'][$k3]['four']=$db_project_funds_money->where($where_f)->order('id desc')->select();
                $result['two'][$k2]['sk_count']+=count($result['two'][$k2]['three'][$k3]['four']);
                foreach($result['two'][$k2]['three'][$k3]['four'] as $k4=>$v4){
                    $result['two'][$k2]['three'][$k3]['four'][$k4]['add_user']=$this->get_username($v4['add_uid']);
                    $where_sk['sid']=$v4['sid'];
                    $where_sk['add_date']=array('elt',$v4['add_date']);
                    $ysk=$db_project_funds_money->where($where_sk)->sum('money');
                    if($v3['money']-$ysk>=0){$result['two'][$k2]['three'][$k3]['four'][$k4]['syysk']=$v3['money']-$ysk;}else{$result['two'][$k2]['three'][$k3]['four'][$k4]['syysk']=0;}
                }
            }
        }
        $result['kp_total']=$total;
        $this->assign('result',$result);
        $this->assign('type',$_REQUEST['type']);
        if($_REQUEST['form']=='funds'){
            $url=__APP__."/Mobile/Financial/project_funds/keyword/".$_REQUEST['keyword']."/mode/".$_REQUEST['mode']."/center_id/".$_REQUEST['center_id']."/type/".$_REQUEST['type']."/ptype/".$_REQUEST['ptype'];
        }elseif($_REQUEST['form']=='total'){
            $url=__APP__."/Mobile/Financial/project_funds_total/keyword/".$_REQUEST['keyword']."/center_id/".$_REQUEST['center_id']."/ptype/".$_REQUEST['ptype']."/start/".$_REQUEST['start']."/end/".$_REQUEST['end'];
        }
        $this->assign('url',$url);
        $this->display();
    }

    //开票&收款汇总表 designed by:wuhuili 2018-06-26
    function project_funds_total(){
        parent::check_permission('KS');
        //赋值
        $date=new \DateTime();
        if(get_permission('KS',2)){
            if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=$this->login_center_id;}
            $this->assign('center_id',$center_id);
        }else{
            if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=-10;}
            $this->assign('center_id',$center_id);
        }
        if(!empty($_REQUEST['ptype'])){$ptype=$_REQUEST['ptype'];}else{$ptype=-10;}
        $this->assign('ptype',$ptype);
        $date->modify('this week');
        if(!empty($_REQUEST['start'])){$start=$_REQUEST['start'];}else{$start=$date->format('Y-m-d');}
        $this->assign('start',$start);
        $date->modify('this week +6 days');
        if(!empty($_REQUEST['end'])){$end=$_REQUEST['end'];}else{$end=$date->format('Y-m-d');}
        $this->assign('end',$end);
        if(!empty($_REQUEST['kp_type'])){$kp_type=$_REQUEST['kp_type'];}else{$kp_type=-10;}
        $this->assign('kp_type',$kp_type);
        if(!empty($_REQUEST['sk_type'])){$sk_type=$_REQUEST['sk_type'];}else{$sk_type=-10;}
        $this->assign('sk_type',$sk_type);
        //上一周期，下一周期
        $this->assign('last_url',__APP__."/Mobile/Financial/project_funds_total/keyword/".$_REQUEST['keyword']."/center_id/".$center_id."/ptype/".$ptype."/start/".$this->start_end_cycle_adjust(1,$start,1,4)."/end/".$this->start_end_cycle_adjust(2,$end,1,4)."/kp_type/".$kp_type."/sk_type/".$sk_type);
        $this->assign('next_url',__APP__."/Mobile/Financial/project_funds_total/keyword/".$_REQUEST['keyword']."/center_id/".$center_id."/ptype/".$ptype."/start/".$this->start_end_cycle_adjust(1,$start,2,4)."/end/".$this->start_end_cycle_adjust(2,$end,2,4)."/kp_type/".$kp_type."/sk_type/".$sk_type);
        //所属中心下拉
        if(get_permission('KS',2)){
            //所属中心下拉
            $pro_list=$this->get_db_universal_type(18,NULL,NULL,NULL,NULL,$this->login_center_id);
        }else{
            //所属中心下拉
            $pro_list=$this->get_db_universal_type(18);
        }
        if($center_id!=-10){
            $db_universal_type=M('db_universal_type');
            $where_c['type']=18;
            $where_c['name2']=$center_id;
            $center_name=$db_universal_type->field('id,name1')->where($where_c)->find();
            $this->assign('center_name',$center_name['name1']);
        }
        $this->assign('pro_list',$pro_list);
        //项目分类下拉菜单
        $ptype_list=$this->library_type_list(6,22);
        $this->assign('ptype_list',$ptype_list[0]['second']);
        if($ptype!=-10){
            $db_universal_label2=M('db_universal_label2');
            $where_p['id']=$ptype;
            $ptype_name=$db_universal_label2->where($where_p)->find();
            preg_match_all("/./us",$ptype_name['second_name'],$matchs);
            $ptype_name_length=count($matchs[0]);
            $this->assign('ptype_name',$ptype_name['second_name']);
            $this->assign('ptype_name_length',$ptype_name_length);
        }
        $db_project_seach=M('db_project_seach');
        if(!empty($_REQUEST['keyword']))
        {
            $map['project_num']=array('like','%'.$_REQUEST['keyword'].'%');
            $map['project_name']=array('like','%'.$_REQUEST['keyword'].'%');
            $map['_logic']='or';
            $where['_complex']=$map;
            $this->assign('keyword',$_REQUEST['keyword']);
            $add_where['center_id']=130010;
            $add_where['keyword']=$_REQUEST['keyword'];
            $add_where['add_uid']=$this->login_id;
            $select_res=$db_project_seach->where($add_where)->find();
            if(empty($select_res)){
                $add_where['add_date']=date('Y-m-d H:i:s');
                $db_project_seach->add($add_where);
            }
        }
        if($center_id!=-10){$where['center_id']=$center_id;}
        if($ptype!=-10){$where['p_type']=$ptype;}
        $db_project=M('db_project');
        $db_project_funds=M('db_project_funds');
        $db_project_funds_stage=M('db_project_funds_stage');
        $db_project_funds_money=M('db_project_funds_money');
        $pro=$db_project->where($where)->field('db_project.*')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->select();
        foreach($pro as $ka=>$va){
            if($ka==0){
                $projects=$va['id'];
            }else{
                $projects.=','.$va['id'];
            }
        }
        if(!empty($projects)){
            $where_s['db_project_funds.pid']=array('in',$projects);
        }else{
            $where_s['db_project_funds.pid']=-1;
        }
        $where_s['db_project_funds.add_date']=array('between',array($start,$end));
        $result=$db_project_funds->field('db_project_funds.*,project_num,project_name,p_type')->where($where_s)->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('db_project_funds.id desc')->select();
        foreach($result as $key=>$value){
            unset($where_k);
            $result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
            //发票
            $where_k['mid']=$value['id'];
            $result[$key]['yikp']=$db_project_funds_stage->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_k)->sum('money')+0;
            $max_stage=$db_project_funds_stage->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_k)->order('stage desc')->find();
            if(empty($max_stage)){$where_k['stage']=array('elt',1);}else{$where_k['stage']=array('elt',$max_stage['stage']);}
            $result[$key]['yinkp']=$db_project_funds_stage->where($where_k)->sum('ticket_money')+0;
            $result[$key]['syykp']=$result[$key]['yinkp']-$result[$key]['yikp'];
            //收款
            $result[$key]['yisk']=$db_project_funds_money->field('db_project_funds_ticket.id')->join('inner join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('inner join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->where($where_k)->order('db_project_funds_ticket.id desc')->sum('db_project_funds_money.money')+0;
            if(empty($result[$key]['yisk'])){$result[$key]['yisk_lj']=-1;}
            $result[$key]['syysk']=$result[$key]['yikp']-$result[$key]['yisk'];
            $where_cs['mid']=$value['id'];
            $count_stage=$db_project_funds_stage->where($where_cs)->count();
            if($count_stage==intval($max_stage['stage'])){
                $result[$key]['title']='当前阶段已是最后一个阶段';
            }else{
                $where_n['mid']=$value['id'];
                if(empty($max_stage)){
                    $where_n['stage']=(intval($max_stage['stage'])+2);
                }else{
                    $where_n['stage']=(intval($max_stage['stage'])+1);
                }
                $next=$db_project_funds_stage->where($where_n)->find();
                $result[$key]['title']='下一阶段应开票金额(日期)：'.($next['ticket_money']+0).'('.$next['ticket_date'].')';

            }
            if(empty($max_stage)){
                $where_on['mid']=$value['id'];
                $where_on['stage']=1;
                $so=$db_project_funds_stage->where($where_on)->find();
                $result[$key]['ticket_date']=$so['ticket_date'];
                $result[$key]['warn_date']=$so['warn_date'];
                $result[$key]['press_date']=$so['press_date'];
                if(strtotime(date("Y-m-d"))>strtotime($so['press_date']) and $result[$key]['yinkp']>$result[$key]['yikp']){$result[$key]['yikp_lj']=-1;}
            }else{
                $result[$key]['ticket_date']=$max_stage['ticket_date'];
                $result[$key]['warn_date']=$max_stage['warn_date'];
                $result[$key]['press_date']=$max_stage['press_date'];
                if(strtotime(date("Y-m-d"))>strtotime($max_stage['press_date']) and $result[$key]['yinkp']>$result[$key]['yikp']){$result[$key]['yikp_lj']=-1;}
            }
            //筛选
            if($kp_type==1){
                if($result[$key]['syykp']>0){
                    unset($result[$key]);
                }
            }elseif($kp_type==-1){
                if($result[$key]['syykp']<=0){
                    unset($result[$key]);
                }
            }
            if($sk_type==1){
                if($result[$key]['syysk']>0){
                    unset($result[$key]);
                }
            }elseif($sk_type==-1){
                if($result[$key]['syysk']<=0){
                    unset($result[$key]);
                }
            }
            $result[$key]['add_user']=$this->get_username($value['add_uid']);
        }
        //合计
        foreach($result as $kk=>$vv){
            $sum['contract_money']+=$vv['contract_money'];
            $sum['yinkp']+=$vv['yinkp'];
            $sum['yikp']+=$vv['yikp'];
            $sum['syykp']+=$vv['syykp'];
            $sum['yisk']+=$vv['yisk'];
            $sum['syysk']+=$vv['syysk'];
        }
        //推荐搜索
        $where_aaa['center_id']=130010;
        $result_t=$db_project_seach->field('keyword,count(id) as ct')->where($where_aaa)->group('center_id,keyword')->order('ct desc,add_date desc')->limit(6)->select();
        $this->assign('result_t',$result_t);
        //搜索历史
        $where_l['center_id']=130010;
        $where_l['add_uid']=$this->login_id;
        $result_l=$db_project_seach->field('keyword,max(add_date) as md')->where($where_l)->group('center_id,keyword')->order('md desc')->limit(6)->select();
        $this->assign('result_l',$result_l);
        $count=count($result);
        $result=array_slice($result, 0,3);
        $this->assign('sum',$sum);
        $this->assign('count',$count);
        $this->assign('result',$result);
        $this->display();
    }

    //开票&收款汇总表 designed by:wuhuili 2018-06-26
    function project_funds_total_ajax(){
        $start_size=$_REQUEST['start_size'];
        $start_page=$_REQUEST['start_page'];
        parent::check_permission('KS');
        //赋值
        $date=new \DateTime();
        if(get_permission('KS',2)){
            if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=$this->login_center_id;}
            $this->assign('center_id',$center_id);
        }else{
            if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=-10;}
            $this->assign('center_id',$center_id);
        }
        if(!empty($_REQUEST['ptype'])){$ptype=$_REQUEST['ptype'];}else{$ptype=-10;}
        $this->assign('ptype',$ptype);
        $date->modify('this week');
        if(!empty($_REQUEST['start'])){$start=$_REQUEST['start'];}else{$start=$date->format('Y-m-d');}
        $this->assign('start',$start);
        $date->modify('this week +6 days');
        if(!empty($_REQUEST['end'])){$end=$_REQUEST['end'];}else{$end=$date->format('Y-m-d');}
        $this->assign('end',$end);
        if(!empty($_REQUEST['kp_type'])){$kp_type=$_REQUEST['kp_type'];}else{$kp_type=-10;}
        $this->assign('kp_type',$kp_type);
        if(!empty($_REQUEST['sk_type'])){$sk_type=$_REQUEST['sk_type'];}else{$sk_type=-10;}
        $this->assign('sk_type',$sk_type);
        //上一周期，下一周期
        $this->assign('last_url',__APP__."/Mobile/Financial/project_funds_total/keyword/".$_REQUEST['keyword']."/center_id/".$center_id."/ptype/".$ptype."/start/".$this->start_end_cycle_adjust(1,$start,1,4)."/end/".$this->start_end_cycle_adjust(2,$end,1,4)."/kp_type/".$kp_type."/sk_type/".$sk_type);
        $this->assign('next_url',__APP__."/Mobile/Financial/project_funds_total/keyword/".$_REQUEST['keyword']."/center_id/".$center_id."/ptype/".$ptype."/start/".$this->start_end_cycle_adjust(1,$start,2,4)."/end/".$this->start_end_cycle_adjust(2,$end,2,4)."/kp_type/".$kp_type."/sk_type/".$sk_type);
        //所属中心下拉
        if(get_permission('KS',2)){
            //所属中心下拉
            $pro_list=$this->get_db_universal_type(18,NULL,NULL,NULL,NULL,$this->login_center_id);
        }else{
            //所属中心下拉
            $pro_list=$this->get_db_universal_type(18);
        }
        $this->assign('pro_list',$pro_list);
        //项目分类下拉菜单
        $ptype_list=$this->library_type_list(6,22);
        $this->assign('ptype_list',$ptype_list[0]['second']);
        if(!empty($_REQUEST['keyword']))
        {
            $map['project_num']=array('like','%'.$_REQUEST['keyword'].'%');
            $map['project_name']=array('like','%'.$_REQUEST['keyword'].'%');
            $map['_logic']='or';
            $where['_complex']=$map;
            $this->assign('keyword',$_REQUEST['keyword']);
        }
        if($center_id!=-10){$where['center_id']=$center_id;}
        if($ptype!=-10){$where['p_type']=$ptype;}
        $db_project=M('db_project');
        $db_project_funds=M('db_project_funds');
        $db_project_funds_stage=M('db_project_funds_stage');
        $db_project_funds_money=M('db_project_funds_money');
        $pro=$db_project->where($where)->field('db_project.*')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->select();
        foreach($pro as $ka=>$va){
            if($ka==0){
                $projects=$va['id'];
            }else{
                $projects.=','.$va['id'];
            }
        }
        if(!empty($projects)){
            $where_s['db_project_funds.pid']=array('in',$projects);
        }else{
            $where_s['db_project_funds.pid']=-1;
        }
        $where_s['db_project_funds.add_date']=array('between',array($start,$end));
        $result=$db_project_funds->field('db_project_funds.*,project_num,project_name,p_type')->where($where_s)->join('left join db_project on db_project_funds.pid=db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->order('db_project_funds.id desc')->select();
        foreach($result as $key=>$value){
            unset($where_k);
            $result[$key]['p_typen']=$this->get_db_universal_label(2,$value['p_type']);
            //发票
            $where_k['mid']=$value['id'];
            $result[$key]['yikp']=$db_project_funds_stage->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_k)->sum('money')+0;
            $max_stage=$db_project_funds_stage->join('inner join db_project_funds_ticket on db_project_funds_stage.id=db_project_funds_ticket.tid')->where($where_k)->order('stage desc')->find();
            if(empty($max_stage)){$where_k['stage']=array('elt',1);}else{$where_k['stage']=array('elt',$max_stage['stage']);}
            $result[$key]['yinkp']=$db_project_funds_stage->where($where_k)->sum('ticket_money')+0;
            $result[$key]['syykp']=$result[$key]['yinkp']-$result[$key]['yikp'];
            //收款
            $result[$key]['yisk']=$db_project_funds_money->field('db_project_funds_ticket.id')->join('inner join db_project_funds_ticket on db_project_funds_money.sid=db_project_funds_ticket.id')->join('inner join db_project_funds_stage on db_project_funds_ticket.tid=db_project_funds_stage.id')->where($where_k)->order('db_project_funds_ticket.id desc')->sum('db_project_funds_money.money')+0;
            if(empty($result[$key]['yisk'])){$result[$key]['yisk_lj']=-1;}
            $result[$key]['syysk']=$result[$key]['yikp']-$result[$key]['yisk'];
            $where_cs['mid']=$value['id'];
            $count_stage=$db_project_funds_stage->where($where_cs)->count();
            if($count_stage==intval($max_stage['stage'])){
                $result[$key]['title']='当前阶段已是最后一个阶段';
            }else{
                $where_n['mid']=$value['id'];
                if(empty($max_stage)){
                    $where_n['stage']=(intval($max_stage['stage'])+2);
                }else{
                    $where_n['stage']=(intval($max_stage['stage'])+1);
                }
                $next=$db_project_funds_stage->where($where_n)->find();
                $result[$key]['title']='下一阶段应开票金额(日期)：'.($next['ticket_money']+0).'('.$next['ticket_date'].')';

            }
            if(empty($max_stage)){
                $where_on['mid']=$value['id'];
                $where_on['stage']=1;
                $so=$db_project_funds_stage->where($where_on)->find();
                $result[$key]['ticket_date']=$so['ticket_date'];
                $result[$key]['warn_date']=$so['warn_date'];
                $result[$key]['press_date']=$so['press_date'];
                if(strtotime(date("Y-m-d"))>strtotime($so['press_date']) and $result[$key]['yinkp']>$result[$key]['yikp']){$result[$key]['yikp_lj']=-1;}
            }else{
                $result[$key]['ticket_date']=$max_stage['ticket_date'];
                $result[$key]['warn_date']=$max_stage['warn_date'];
                $result[$key]['press_date']=$max_stage['press_date'];
                if(strtotime(date("Y-m-d"))>strtotime($max_stage['press_date']) and $result[$key]['yinkp']>$result[$key]['yikp']){$result[$key]['yikp_lj']=-1;}
            }
            //筛选
            if($kp_type==1){
                if($result[$key]['syykp']>0){
                    unset($result[$key]);
                }
            }elseif($kp_type==-1){
                if($result[$key]['syykp']<=0){
                    unset($result[$key]);
                }
            }
            if($sk_type==1){
                if($result[$key]['syysk']>0){
                    unset($result[$key]);
                }
            }elseif($sk_type==-1){
                if($result[$key]['syysk']<=0){
                    unset($result[$key]);
                }
            }
            $result[$key]['add_user']=$this->get_username($value['add_uid']);
        }
        //合计
        foreach($result as $kk=>$vv){
            $sum['contract_money']+=$vv['contract_money'];
            $sum['yinkp']+=$vv['yinkp'];
            $sum['yikp']+=$vv['yikp'];
            $sum['syykp']+=$vv['syykp'];
            $sum['yisk']+=$vv['yisk'];
            $sum['syysk']+=$vv['syysk'];
        }
        $count=count($result);
        $result=array_slice($result,$start_size,$start_page);
        echo json_encode($result);die;
    }

    //项目款项-->清空历史操作 designed by:wuhuili 2017-12-22
    public function project_funds_del(){
        $db_project_seach=M('db_project_seach');
        $where['center_id']=$_REQUEST['center_id'];
        $where['add_uid']=$this->login_id;
        $db_project_seach->where($where)->delete();
    }

}
?>